package cn.chinaflame.pagodas.utils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.io.Serializable;
import java.security.SecureRandom;
import java.util.UUID;

//import org.activiti.engine.impl.cfg.IdGenerator;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
public class IDGen {
	
	 /** 
     * 20位末尾的数字id 
     */  
    public static int Guid=100;  
    private static SecureRandom random = new SecureRandom();
	
    public static String getGuid() {  
          
    	IDGen.Guid+=1;  

        long now = System.currentTimeMillis();    
        //获取4位年份数字    
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");    
        //获取时间戳    
        String time=dateFormat.format(now);  
        String info=now+"";  
        //获取三位随机数    
        //int ran=(int) ((Math.random()*9+1)*100);   
        //要是一段时间内的数据连过大会有重复的情况，所以做以下修改  
        int ran=0;  
        if(IDGen.Guid>999){  
        	IDGen.Guid=100;         
        }  
        ran=IDGen.Guid;  
                  
        return time+info.substring(2, info.length())+ran;    
    }  
    private static long num=0; 
	   
	  /**
	   * 随机生成UUID
	   * @return
	   */
	  public static synchronized String getUUID(){
	    UUID uuid=UUID.randomUUID();
	    String str = uuid.toString(); 
	    String uuidStr=str.replace("-", "");
	    return uuidStr;
	  }
	  /**
	   * 根据字符串生成固定UUID
	   * @param name
	   * @return
	   */
	  public static synchronized String getUUID(String name){
	    UUID uuid=UUID.nameUUIDFromBytes(name.getBytes());
	    String str = uuid.toString(); 
	    String uuidStr=str.replace("-", "");
	    return uuidStr;
	  }
	  /**
	   * 根据日期生成长整型id
	   * @param args
	   */
	  public static synchronized long getLongId(){
	    String date=DateTimes.format(new Date(), "yyyyMMddHHmmssS");
	    System.out.println("原始id="+date);
	    if(num>=99) num=0l;
	    ++num;
	    if(num<10) {
	      date=date+00+num;
	    }else{
	      date+=num;
	    }
	    return Long.valueOf(date);
	  }
	  final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',  
		        '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',  
		        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',  
		        'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',  
		        'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',  
		        'Z' }; 
	public final static char[] chars={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',  
		        'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z'};
	  final static char[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9' };  
	  
	 /* public static String groupCode(){
		  
	  }*/
		/** 
		 * 随机ID生成器，由数字、小写字母和大写字母组成 
		 *  
		 * @param size 
		 * @return 
		 */  
		public static String code(int size) {  
		    Random random = new Random();  
		    char[] cs = new char[size];  
		    for (int i = 0; i < cs.length; i++) {  
		        cs[i] = digits[random.nextInt(digits.length)];  
		    }  
		    return new String(cs);  
		} 
		/** 
		 * 随机ID生成器，由数字、小写字母和大写字母组成 
		 *  
		 * @param size 
		 * @return 
		 */  
		public static String number(int size) {  
		    Random random = new Random();  
		    char[] cs = new char[size];  
		    for (int i = 0; i < cs.length; i++) {  
		        cs[i] = numbers[random.nextInt(numbers.length)];  
		    }  
		    return new String(cs);  
	   } 
		
		/**
		 * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
		 */
		public static String uuid() {
			return UUID.randomUUID().toString().replaceAll("-", "");
		}
		
		/**
		 * 使用SecureRandom随机生成Long. 
		 */
		public static long randomLong() {
			return Math.abs(random.nextLong());
		}

		/**
		 * 基于Base62编码的SecureRandom随机生成bytes.
		 */
		/*public static String randomBase62(int length) {
			byte[] randomBytes = new byte[length];
			random.nextBytes(randomBytes);
			return Encodes.encodeBase62(randomBytes);
		}*/
		
		/**
		 * Activiti ID 生成
		 */
		public String getNextId() {
			return IDGen.uuid();
		}

		/*public Serializable generateId(Session session) {
			return IDGenerator.uuid();
		}*/
		
		public static void main(String[] args) {
			System.out.println(IDGen.uuid());
			System.out.println(IDGen.uuid().length());
			System.out.println(new IDGen().getNextId());
			for (int i=0; i<1000; i++){
				System.out.println(IDGen.randomLong() + "  " );// IDGenerator.randomBase62(5)
			}
			System.out.println(getLongId());
		}
	  
}
